/**
  * Created by Joshua Conero(O'Uymas) on 2016/12/6 0006.
  * QQ: 957376074
  * E-mail: Brximl@163.com
  * 工具菜单内容的实现
  */
package org.yang
import java.net.{InetAddress, URL}
import scala.swing.Swing._
import scala.swing.TabbedPane.Page
import scala.swing._
import scala.swing.event.ButtonClicked
import collection.JavaConversions._ // 用于循环java对象
class tool(tab:TabbedPane,itemId:String = "") {
  val br = "\r\n"
  var helper = new Common()
  val ipt = new TextField(30){// 长度设置为10
    name = "url"
    editable = true
  }

  val title = itemId match {
    case "ip" => "工具/IP"
    case "url"=> "工具/URL"
    case "log_btn" => "工具/日志"
  }
  // 主页内容
  val mainPage = itemId match{
    case "ip"  => ipUrlMainPage()
    case "url" => ipUrlMainPage("url")
    case "log_btn" => logMainPage()
  }
  // 菜单按钮
  val leftMenu = new FlowPanel{
    border = Swing.EmptyBorder(5,5,5,5)
    val ipbtn = new Button("ip")
    val urlbtn = new Button("url")
    contents += new BoxPanel(Orientation.Vertical){
      contents += ipbtn
      contents += urlbtn
    }
  }
  tab.pages += new Page(title,new SplitPane(Orientation.Vertical,leftMenu,mainPage){
    continuousLayout = true
  })
  // ip 界面
  def ipUrlMainPage(typeId:String = "ip"):FlowPanel = {
    new FlowPanel{
      border = Swing.EmptyBorder(5,5,5,5)
      val ipConsole = new TextArea(){
        columns = 400
        rows = 100
        preferredSize = new Dimension(550,400)
        border = Swing.EmptyBorder(5,5,5,5)
      }
      val jc = new ScrollPane(){
        contents = ipConsole
        preferredSize = new Dimension(400,300)
        border = Swing.EmptyBorder(5,5,5,5)
      }
      contents += new BoxPanel(Orientation.Horizontal){
        border = CompoundBorder(TitledBorder(EtchedBorder, "url输入选项"), EmptyBorder(5,100,20,300))
        val savebtn = new Button("确定")
        val rip = new RadioButton("ip")
        val rurl = new RadioButton("url")
        val mutex = new ButtonGroup(rip,rurl)
        if(typeId == "ip"){
          mutex.select(rip)
        }
        else{
          mutex.select(rurl)
        }
        contents ++= mutex.buttons
        contents += ipt
        contents += savebtn
        listenTo(savebtn)
        reactions += {
          case ButtonClicked(`savebtn`) =>
            val rChk = mutex.selected.get.text.toString
            val site = ipt.text.trim
            rChk match{
              case "ip" =>
                var loc = InetAddress.getLoopbackAddress
                if(site.trim != "") loc = InetAddress.getByName(site)
                var old = ipConsole.text
                val txt = br+"CanonicalHostName："+loc.getCanonicalHostName+
                  br+"HostAddress："+loc.getHostAddress+
                  br+"HostName："+loc.getHostName+
                  br+"HostName："+loc.getHostName+
                  br+"Address： "+loc.getAddress.mkString
                old = br+"----------------"+helper.curdate()+"-----------------"+
                  txt+
                  br+"--------------------- end -------------------------"+
                  old
                ipConsole.text = old
              case _ =>
                if(site == "") ipConsole.text = "url地址不可为空"
                else{
                  val u = new URL(site)
                  ipConsole.text = "------------------url 静态信息["+helper.sysdate()+"]------------------"+
                    br+"authority: "+ u.getAuthority +
                    br+"file: " + u.getFile +
                    br+"host: " + u.getHost +
                    br+"port: " + u.getPort +
                    br+"path: " + u.getPath +
                    br+"protocol: " + u.getProtocol +
                    br+"query: " + u.getQuery +
                    br+"reference: " + u.getRef +
                    br+"userInfo: " + u.getUserInfo

                  val conn = u.openConnection()
                  val header = conn.getHeaderFields.keySet()
                  var connInfo = br+"------------------url 连接信息------------------"+
                    br+"mime: "+ conn.getContentType+
                    br+"date: "+ conn.getDate.toString
                  br+"timeout:" + conn.getConnectTimeout.toString
                  br+" 头部信息如下："
                  for(k<- header){
                    connInfo += br+k+ ": "+ conn.getHeaderField(k)
                  }
                  ipConsole.append(connInfo)
                }
            }
        }

      }
      contents += new BoxPanel(Orientation.Horizontal){
        border = CompoundBorder(TitledBorder(EtchedBorder, "运行信息框"), EmptyBorder(5,5,5,5))
        //contents += ipConsole
        contents += jc
        preferredSize = (500,500)
      }
    }
  }
  def logMainPage():FlowPanel = {
    new FlowPanel(){
      val file = helper.dir+(helper.getAppConf\"runtime"\"@logFile").toString()
      contents += new BoxPanel(Orientation.Horizontal){
        val logContent = new TextArea(){
          text = helper.get_content(file)
          preferredSize = new Dimension(550,400)
        }
        val jc = new ScrollPane(){
          contents = logContent
          preferredSize = new Dimension(400,300)
          border = Swing.EmptyBorder(5,5,5,5)
        }
        contents += jc
        preferredSize = (500,500)
      }
    }
  }
}
